<template>
  <div class="personal-info-container">
    <!-- 标题 -->
    <div class="title">
      <img src="@/assets/images/tag.png" class="title-icon" >
      <span>个人资料</span>
    </div>
    <!-- 个人信息展示 -->
    <div class="info-section">
      <div class="title1">
        <span>个人信息</span>
      </div>
      <div class="info-item">
        <label class="label">用户名：</label>
        <span>{{ userInfo?.user?.nickName || '未设置'  }}</span>
      </div>
      <div class="info-item">
        <label class="label">身份：</label>
        <span>{{userInfo?.user?.roles[0]?.roleName || '未设置'}}</span>
      </div>
      <div class="avater1">
        <div class="label1">头像：</div>
        <userAvatar />
      </div>
    </div>
    <!-- 修改密码表单 -->
    <div class="password-section">
      <div class="title1">
        <span>修改登录密码</span>
      </div>
      <el-form ref="pwdRef" :model="user" :rules="rules" label-width="80px">
        <el-form-item label="旧密码" prop="oldPassword">
          <el-input v-model="user.oldPassword" placeholder="请输入旧密码" type="password" show-password />
        </el-form-item>
        <el-form-item label="新密码" prop="newPassword">
          <el-input v-model="user.newPassword" placeholder="请输入新密码" type="password" show-password />
        </el-form-item>
        <el-form-item label="确认密码" prop="confirmPassword">
          <el-input v-model="user.confirmPassword" placeholder="请确认新密码" type="password" show-password/>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submit">保存</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- 提示框 -->
    <el-notification
        v-model="showSaveSuccess"
        title="提示"
        message="保存成功"
        type="success"
        :duration="3000"
    />
  </div>
</template>

<script setup>
import userAvatar from "./userAvatar"
import { ref, reactive, onMounted } from 'vue';
import {getInfo} from "@/api/login.js";
import {message} from "ant-design-vue";
import { updateUserPwd } from "@/api/system/user"

const userInfo = ref({});

onMounted(async () => {
  try {
    const res = await getInfo();
    userInfo.value = res;
    console.log(res)
    if (!userInfo.value) {
      throw new Error("返回数据为空");
    }
    console.log(userInfo.value.user.roles[0].roleName)
  } catch (error) {
    console.error('获取站点信息异常:', error);
    message.error('网络错误，请稍后重试');
  }
});
const { proxy } = getCurrentInstance()


const user = reactive({
  oldPassword: undefined,
  newPassword: undefined,
  confirmPassword: undefined
})

const equalToPassword = (rule, value, callback) => {
  if (user.newPassword !== value) {
    callback(new Error("两次输入的密码不一致"))
  } else {
    callback()
  }
}

const rules = ref({
  oldPassword: [{ required: true, message: "旧密码不能为空", trigger: "blur" }],
  newPassword: [{ required: true, message: "新密码不能为空", trigger: "blur" }, { min: 6, max: 20, message: "长度在 6 到 20 个字符", trigger: "blur" }, { pattern: /^[^<>"'|\\]+$/, message: "不能包含非法字符：< > \" ' \\\ |", trigger: "blur" }],
  confirmPassword: [{ required: true, message: "确认密码不能为空", trigger: "blur" }, { required: true, validator: equalToPassword, trigger: "blur" }]
})
/** 提交按钮 */
function submit() {
  proxy.$refs.pwdRef.validate(valid => {
    if (valid) {
      updateUserPwd(user.oldPassword, user.newPassword).then(response => {
        proxy.$modal.msgSuccess("修改成功")
      })
    }
  })
}
</script>

<style scoped>
.personal-info-container {
  padding: 20px;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
.title {
  color:#4F4C4C;
  font-size: 18px;
  font-weight: 500;
  margin-bottom: 30px;
  display: flex;
  align-items: center;
}
.title-icon{
  width: 7px;
  height: 20px;
  margin-right: 10px;
}
.title1{
  color:black;
  font-size: 16px;
  font-weight: 600;
  margin-bottom: 30px;
}
.info-section {
  margin-bottom: 20px;
}
.info-item {
  margin-bottom: 50px;
  color:#4F4C4C ;
  font-size: 16px;
}
.avater1{
  display: flex;
}
.label {
  display: inline-block;
  width: 80px;
  text-align: left;
  margin-right: 20px;
  font-weight: 500;
}
.label1 {
  display: inline-block;
  width: 80px;
  text-align: left;
  line-height: 110px;
  margin-right: 20px;
  font-weight: 500;
}
.password-section {
  margin-top: 20px;
}
.password-tips {
  margin-top: 5px;
  color: #999;
  font-size: 12px;
}
.strength-bar {
  width: 100px;
  height: 8px;
  background-color: #eee;
  border-radius: 4px;
  overflow: hidden;
  margin: 5px 0;
}
.strength-level {
  height: 8px;
  border-radius: 4px;
}
</style>